package ThreadDemo1;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-10
 * Time: 23:21
 */
public class ThreadDemo21 {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
        //生产销毁者模型
        Thread productor = new Thread(() -> {
            int count = 0;
            while (true) {
                count++;
                try {
                    blockingQueue.put(count);
                    System.out.println("生产者生产: " + count);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        productor.start();


        Thread constomer = new Thread(() -> {
            while (true) {
                try {
                    Integer res = blockingQueue.take();
                    System.out.println("消费者消费: " + res);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        constomer.start();
    }
}
